<template>
  <div class="app-container">
    <div v-if="authUser" style="text-align: center">请登录油企账号查看~</div>
    <div v-else class="main">
      <div class="list">
        <h4>未启用</h4>
        <div v-if="cardListClose.length === 0" style="color: #999">
          空空如也，暂时无停用功能
        </div>
        <div class="cardList">
          <el-card
            shadow="hover"
            v-for="(item, index) in cardListClose"
            :key="item.id"
            style="width: 20rem; height: 10rem"
          >
            <div class="title">
              {{ item.name }}
              <div
                v-if="item.name === '秒杀'"
                style="
                  border-radius: 12px;
                  margin-left: 10px;
                  height: 24px;
                  line-height: 20px;
                  font-size: 12px;
                  background-color: rgb(221, 88, 88);
                  padding: 2px 10px;
                  color: #fff;
                "
              >
                NEW
              </div>
            </div>

            <div class="text">
              <p>{{ item.funcDesc }}</p>
              <el-button @click="open(item)" size="normal">启用</el-button>
            </div>
          </el-card>
        </div>
      </div>

      <div class="list">
        <h4>已启用</h4>
        <div v-if="cardListOpen.length === 0" style="color: #999">
          空空如也，暂时无启用功能
        </div>
        <div class="cardList">
          <el-card
            shadow="hover"
            v-for="(item, index) in cardListOpen"
            :key="item.id"
            style="width: 20rem; height: 10rem"
            v-show="item.name != '充值订单'"
          >
            <div class="title">{{ item.name }}</div>
            <div class="text">
              <p>{{ item.funcDesc }}</p>
              <el-button @click="close(item)" size="normal">停用</el-button>
            </div>
          </el-card>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {
  getFunctionListApi,
  updateFunctionConfigStatusApi,
} from "@/api/MenuSetting/functionSetting";
export default {
  name: "FunctionSetting",
  data() {
    return {
      cardListOpen: [],
      cardListClose: [],
    };
  },
  created() {
    if (!this.authUser) {
      this.getList();
      console.log(111, this.cardListClose);
    }
  },
  computed: {
    authUser() {
      let roles = this.$store.state.user.roles;
      if (roles.includes("admin")) {
        return true;
      } else {
        return false;
      }
    },
  },
  methods: {
    // 获取列表
    async getList() {
      const res = await getFunctionListApi({ enterpriseId: this.enterpriseId });
      this.cardListClose = res.data.close || [];
      this.cardListOpen = res.data.open || [];
      console.log(this.cardListClose, "1111111111");
      console.log(this.cardListOpen, "22222222222");
    },

    // 停用
    close(item) {
      console.log("close");
      const obj = {
        id: item.id,
        status: 0,
      };
      this.$confirm("确定停用？停用后改功能将不能使用", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        await this.updateFunctionConfigStatus(obj);
        this.msgSuccess("已停用");
        this.getList();
      });
    },

    // 启用
    open(item) {
      console.log("open");
      const obj = {
        id: item.id,
        status: 1,
      };
      this.$confirm("确定开启？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        await this.updateFunctionConfigStatus(obj);
        this.msgSuccess("已开启");
        this.getList();
      });
    },

    // 改变功能菜单的状态
    async updateFunctionConfigStatus(obj) {
      await updateFunctionConfigStatusApi(obj);
    },
  },
};
</script>
<style lang="scss" scoped>
.main {
  display: flex;
  flex-direction: column;
  .list {
    margin-bottom: 40px;
    .cardList {
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      .el-card {
        margin-bottom: 10px;
        margin-right: 20px;
        box-shadow: 3px 3px 4px rgba(185, 185, 185, 0.5);
        &:hover {
          box-shadow: 4px 4px 8px rgba(185, 185, 185, 0.5);
          transform: scale(1.04);
        }
      }
      .title {
        font-size: 18px;
        display: flex;
        align-items: center;
      }
      .text {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        p {
          color: #999;
          width: 70%;
          font-size: 14px;
          text-align: justify;
        }
      }
    }
  }
}
</style>
